<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class generate_control extends CI_Model
{
    private $controllersDir = '';
    private $module = '';
    public function __construct()
    {
        parent::__construct();
        $this->controllersDir = $_SERVER['DOCUMENT_ROOT'].'/application/modules/generate/scripts/BO';
        $this->module = 'admin';
    }
    private function form_Filter()
    {   
        //pages_type pid, article idel
        $table  = $_SESSION['gene']['table'];
        $str = '';
        $filter = '';
        $arrKey = explode(';', $_SESSION['gene']['fk']);
        
        foreach ($arrKey as $val) {
            $val = trim($val);
             $last = strrchr($val, ' '); // last
             $last = trim($last);
             $fist = substr($val, 0, strpos($val, ' ')); // fist
             $fist = trim($fist);
    $str .= '
  public function filter'.$fist.'()
  {
    $id = $_POST["'.$last.'"];
    if (isset($_POST["btnReset"])) 
        $this->session->unset_userdata("'.iprefix.'_filter_'.$fist.'");
    else 
        $this->session->set_userdata("'.iprefix.'_filter_'.$fist.'", $id);
    redirect(site_url("'.$table.'"));
  }';
        }
        return $str;
    }
    function main()
    {
        $table = $_SESSION['gene']['table'];
$str = '<?php if ( ! defined("BASEPATH")) exit("No direct script access allowed");
class '.$table.' extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("'.$table.'_model");
    }
  
    public function sort($field = "created", $order = "desc")
    {
        $this->'.$table.'_model->dataSort($field, $order);
        $this->index();
    }

    public function index($target = 0)
    {
        $arr = $this->'.$table.'_model->dataIndex($target); 
        $breakCrum = \'<a href="\'.base_url().\'">\'.t("Home").\'</a> » <span>\'.t("'.$table.'").\'</span>\';
        $data = array("content" => $this->load->view("'.$table.'/main", $arr ,true), "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    } 
  
    public function find()
    {
        if (!isset($_POST["txt_find"])) return redirect(site_url("'.$table.'/index"));
        if (isset($_POST["btnReset"]) && $_POST["btnReset"] == "Reset") 
            $this->session->unset_userdata("'.iprefix.'_find_'.$table.'");
        else {
            $keyword = $this->input->post("txt_find", true);
            if ($keyword != "") {
                notify_set_messages(notify_info(t("Result")." ".$keyword), "'.$table.'");
                $this->session->set_userdata("'.iprefix.'_find_'.$table.'", $keyword);
            }
        }
    
        redirect(site_url("'.$table.'/index"));
    }';
if ($_SESSION['gene']['fk'] != '') $str .= $this->form_Filter();
$str .= '    
    public function add()
    {        
        $arr = $this->'.$table.'_model->dataAdd();
        $breakCrum = \'<a href="\'.base_url().\'">\'.t("Home").\'</a> » 
        <a href="/'.$this->module.'/'.$table.'">\'.t("'.$table.'").\'</a> » <span>\'.t("Add").\'</span>\';
        $data = array("content" => $this->load->view("'.$table.'/add", $arr, true), 
                      "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }    
  
    public function edit($id)
    {       
        $arr = $this->'.$table.'_model->dataEdit($id);
        $breakCrum = \'<a href="\'.base_url().\'">\'.t("Home").\'</a> » 
        <a href="/'.$this->module.'/'.$table.'">\'.t("'.$table.'").\'</a> » <span>\'.t("Edit").\'</span>\';
        $data = array("content" => $this->load->view("'.$table.'/edit", $arr, true),
                "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }
  
    public function doAdd()
    {
        if(!$this->'.$table.'_model->checkForm()) {
            notify_set_messages(notify_error(validation_errors()), "'.$table.'");
            return $this->add();
        }
        if ($this->'.$table.'_model->doUpdate()) {
            notify_set_messages(notify_success(t("Record is added")), "'.$table.'");
            return redirect(site_url("'.$table.'"));    
        }
      
    }
  
    public function doEdit()
    {        
        if(!$this->'.$table.'_model->checkForm()) {
            notify_set_messages(notify_error(validation_errors()), "'.$table.'");
            return $this->edit(decrypt($_POST["idx"]));        
        }
        if ($this->'.$table.'_model->doUpdate()) {
            notify_set_messages(notify_success(t("Record is updated")), "'.$table.'");
            redirect(site_url("'.$table.'"));   
        }
     
    } ';

$str .= $this->idel();    
$str .= '    
}';
    $dir = $this->controllersDir.'/'.$table.'.php';
    if (file_exists($dir)) delete_files($dir);
    write_file($dir, $str, 'w');
    }
    
    function idel()
    {
        $table = $_SESSION['gene']['table'];
        $fields = $this->db->list_fields($table);
        $flag = false;
        foreach ($fields as $field) {
           if (trim($field) == 'idel') $flag = true;
        }
        $str = '
  
    public function del($id)
    {
        $result = $this->'.$table.'_model->del($id);
        if (is_array($result) && $result[0] == -1) {
            notify_set_messages(notify_atten($result[1]), "'.$table.'");
        } else {
            notify_set_messages(notify_success(t("Record is deleted")), "'.$table.'");
        }
        redirect(site_url("'.$table.'"));
    }     
    ';
        return $str;
    }

}